package ui;

import DataBean.Basic;
import DataBean.Info;
import VcardTest.CSVIO;
import VcardTest.VCardIO;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.border.StandardBorderPainter;
import org.jvnet.substance.button.ClassicButtonShaper;
import org.jvnet.substance.painter.StandardGradientPainter;
import org.jvnet.substance.skin.*;

import org.jvnet.substance.skin.AutumnSkin;
import org.jvnet.substance.theme.SubstanceTerracottaTheme;
import org.jvnet.substance.watermark.SubstanceBinaryWatermark;
import u.Pinyin;

/**
 *
 * @author Touching
 */
public class MainFrame extends javax.swing.JFrame {

    ArrayList<Info> list = new ArrayList();
    int isInit = 0;
    String[] groupname;

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        Info sign = new Info();
        list.add(sign);
        initComponents();
        System.out.println("first list size:" + list.size());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuItem5 = new javax.swing.JMenuItem();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        search = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        importCSV = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        importVCARD = new javax.swing.JMenuItem();
        export = new javax.swing.JMenuItem();

        jMenuItem5.setText("jMenuItem5");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("通讯录");
        setBackground(new java.awt.Color(204, 255, 204));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "姓名", "手机", "生日", "所属组", "备注", "电子邮箱"
            }
        ));
        jScrollPane2.setViewportView(jTable);

        jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel1.setText("按关键字查询：");

        search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchActionPerformed(evt);
            }
        });
        search.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                searchKeyTyped(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "全部联系人" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel2.setText("分组显示：");

        jButton5.setText("确定");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(search, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(jButton5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(search, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jButton5))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel5.setText("个联系人");

        jLabel3.setText("共：");

        jLabel4.setText("0");

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jButton1.setIcon(new javax.swing.ImageIcon("E:\\java课程设计\\work\\Java\\Contacts\\new.png")); // NOI18N
        jButton1.setToolTipText("新增联系人");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon("E:\\java课程设计\\work\\Java\\Contacts\\delete.png")); // NOI18N
        jButton2.setToolTipText("删除联系人");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton2MouseReleased(evt);
            }
        });
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon("E:\\java课程设计\\work\\Java\\Contacts\\check.png")); // NOI18N
        jButton3.setToolTipText("查看联系人详细信息");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jButton4.setIcon(new javax.swing.ImageIcon("E:\\java课程设计\\work\\Java\\Contacts\\modify.png")); // NOI18N
        jButton4.setToolTipText("修改联系人信息");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(92, 92, 92)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(95, 95, 95)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(85, 85, 85)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(110, 110, 110))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addContainerGap())
        );

        importCSV.setText("文件");
        importCSV.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                importCSVMouseClicked(evt);
            }
        });

        jMenuItem1.setText("导入csv通讯录文件");
        jMenuItem1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jMenuItem1MousePressed(evt);
            }
        });
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        importCSV.add(jMenuItem1);

        importVCARD.setText("导入vCard通讯录文件");
        importVCARD.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                importVCARDMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                importVCARDMousePressed(evt);
            }
        });
        importCSV.add(importVCARD);

        export.setText("导出通讯录");
        export.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                exportMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                exportMousePressed(evt);
            }
        });
        importCSV.add(export);

        jMenuBar1.add(importCSV);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void importVCARDMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_importVCARDMouseClicked


    }//GEN-LAST:event_importVCARDMouseClicked

    private void importCSVMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_importCSVMouseClicked

    }//GEN-LAST:event_importCSVMouseClicked

    private void importVCARDMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_importVCARDMousePressed

        //导入VCard
        VCardIO io = new VCardIO();

        int pl = list.size();
        try {
            io.ReadVCard(list,1);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        jTable.setRowSorter(new TableRowSorter<TableModel>(jTable.getModel()));
        jLabel4.setText(Integer.toString(jTable.getRowCount()));
        groupname = divgroup(list);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(groupname));
    }//GEN-LAST:event_importVCARDMousePressed

    private void jMenuItem1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem1MousePressed

        //导入csv
        CSVIO io = new CSVIO();
        int pl = list.size();
        io.ReadCSV(list, 1);
        jLabel4.setText(Integer.toString(jTable.getRowCount()));
        groupname = divgroup(list);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(groupname));

    }//GEN-LAST:event_jMenuItem1MousePressed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated

        DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
        int rowCount = tableModel.getRowCount();
        for (int m = rowCount - 1; m >= 0; m--) {
            tableModel.removeRow(m);
        }
        if (this.isInit == 0) {
            try {
                VCardIO io = new VCardIO();
                io.ReadVCard(list,0);
                this.isInit = 1;
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        int l = list.size();
        Basic[] b = new Basic[l];
        for (int i = 1; i < l; i++) {
            b[i] = list.get(i).getBec();
            String[] array = new String[6];
            array[0] = b[i].getName();
            array[1] = b[i].getMobile();
            array[2] = b[i].getBirth();
            array[3] = b[i].getGroup();
            array[4] = b[i].getNote();
            array[5] = b[i].getMailbox();
            tableModel.addRow(array);
        }

        jTable.invalidate();
        jLabel4.setText(Integer.toString(tableModel.getRowCount()));
        groupname = divgroup(list);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(groupname));

    }//GEN-LAST:event_formWindowActivated

    private void exportMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_exportMouseClicked

    }//GEN-LAST:event_exportMouseClicked

    private void exportMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_exportMousePressed
        // 导出通讯录
        VCardIO io = new VCardIO();

        io.WriteVCard(list,1);



    }//GEN-LAST:event_exportMousePressed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed

        // 分组下拉菜单
        int temp = jComboBox1.getSelectedIndex();
        String text = groupname[temp];
        search.requestFocusInWindow();
        //System.out.println("test " + text + t.matches("[\\w].*"));
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(jTable.getModel());
        jTable.setRowSorter(sorter);
        //System.out.println("length= " + text.length());
        if (temp == 0) {
            sorter.setRowFilter(null);
        } else {
            sorter.setRowFilter(RowFilter.regexFilter(text));
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void searchKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_searchKeyTyped

        //搜索
        String t = evt.getKeyChar() + "";
        String text = null;
        if (t.matches("[\\w].*")) {
            text = search.getText() + t;
        } else {
            text = search.getText();
        }
        search.requestFocusInWindow();
        //System.out.println("test " + text + t.matches("[\\w].*"));
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
                jTable.getModel());
        //System.out.println("length= " + text.length());
        if (text.length() == 0) {
            ((TableRowSorter) this.jTable.getRowSorter()).setRowFilter(null);
        } else {
            ((TableRowSorter) this.jTable.getRowSorter()).setRowFilter(RowFilter.regexFilter(text));
        }
    }//GEN-LAST:event_searchKeyTyped

    private void searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchActionPerformed
       String pinyin = search.getText();
                  ArrayList qp = new ArrayList();
    ArrayList szm = new ArrayList();
    for(int i = 0;i<list.size();i++){
        String n = list.get(i).getBec().getName();
        Pinyin.quanpin(n);
        Pinyin.szm(n);
        String quanpin = delSpace(Pinyin.quanpin(n));
        String firstpin = delSpace(Pinyin.szm(n));
        qp.add(i, quanpin);
        szm.add(i, firstpin);
//        System.out.println("pinyin "+ Pinyin.getFullSpell(n));       
    }
    
    int p =0;   
    for(int k=0;k<qp.size();k++){
    
        if(pinyin.equals(qp.get(k))){
   
            p =k;
        }
    }
    for(int k=0;k<szm.size();k++){

        if(pinyin.equals(szm.get(k))){
 
            p =k;
        }
    }
         pinyin = null;
    System.out.println("p="+p); 
    DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
        tableModel.setRowCount(list.size());
            for (int i = tableModel.getRowCount() - 1; i >= 0; i--) {
            tableModel.removeRow(i);//清空表格
            }
            String[] member = new String[5];
            member[0] = list.get(p).getBec().getName();
            member[1] = list.get(p).getBec().getMobile();
            member[2] = list.get(p).getBec().getBirth();
            member[3] = list.get(p).getBec().getMailbox();
            member[4] = list.get(p).getBec().getGroup();
            System.out.println("成员1:"+member[0]);
            System.out.println("成员2:"+member[1]);
            System.out.println("成员3:"+member[2]);
            System.out.println("成员4:"+member[3]);
            System.out.println("成员5:"+member[4]);
            
            tableModel.addRow(member);       
            jTable.invalidate();

    }//GEN-LAST:event_searchActionPerformed

    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseClicked

        // 修改联系人信息
        int i[] = jTable.getSelectedRows();

        DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
        int[] row = getSelectRow(i);
        int row2change = row[0];

        NewCont newc = new NewCont(list, 1, row2change);
        newc.paint(list, 1, row2change);

    }//GEN-LAST:event_jButton4MouseClicked

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked

        //显示联系人详细信息
        int i[] = jTable.getSelectedRows();

        DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
        int[] row = getSelectRow(i);
        int row2show = row[0];
        NewCont newc = new NewCont(list, 2, row2show);
        newc.paint(list, 2, row2show);

    }//GEN-LAST:event_jButton3MouseClicked

    private void jButton2MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2MouseReleased

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked

        //删除联系人
        int i[] = jTable.getSelectedRows();

        DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
        int[] select = getSelectRow(i);
        for (int j = i.length - 1; j >= 0; j--) {
            tableModel.removeRow(i[j]);
        }
        for (int k = 0; k < select.length; k++) {

            list.remove(select[k]);
        }

        jLabel4.setText(Integer.toString(jTable.getRowCount()));
    }//GEN-LAST:event_jButton2MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked

        //新增联系人
        NewCont newc = new NewCont(list, 0, 0);
        newc.paint(list, 0, 0);
        Toolkit tk = Toolkit.getDefaultToolkit();
        Image image = tk.createImage("logo.png"); /*image.gif是你的图标*/

        newc.setIconImage(image);
    }//GEN-LAST:event_jButton1MouseClicked

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
      /*  
        VCardIO io=new VCardIO();
        io.WriteVCard(list,0);
        System.exit(0);*/
    }//GEN-LAST:event_formWindowClosing

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:

      
               int j = 0;
               int [] p = null;
          String pinyin = search.getText();
        ArrayList qp = new ArrayList();
        ArrayList szm = new ArrayList();
    for(int i = 0;i<list.size();i++){
        System.out.println("列表中的名字为"+list.get(i).getBec().getName());
        String n = list.get(i).getBec().getName();
        Pinyin.quanpin(n);
        Pinyin.szm(n);
        String quanpin = delSpace(Pinyin.quanpin(n));
        String firstpin = delSpace(Pinyin.szm(n));
        qp.add(i, quanpin);
        szm.add(i, firstpin);
//        System.out.println("pinyin "+ Pinyin.quanpin(n));       
    }
    
      for(int k=1;k<qp.size();k++){
        System.out.println("listlist"+list.get(k).getBec().getName());

        if (pinyin.equals(qp.get(k))) {
  
            p[j]=k;
            j++;
             System.out.println(p);
         } 
    }
  
     
    for(int k=1;k<qp.size();k++){

        if (pinyin.equals(qp.get(k))) {

            p[j]=k;
            j++;
             System.out.println(p);
         } 
    }
  
  
        DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
        tableModel.setRowCount(list.size());
        
        
        
            for (int i = tableModel.getRowCount() - 1; i >= 0; i--) {
            tableModel.removeRow(i);//清空表格

            }
           System.out.println("清空表格后" + tableModel.getRowCount());
        for (int i = 1; i < list.size(); i++) {
            String[] member = new String[6];       
            member[0] = list.get(i).getBec().getName();
            member[1] = list.get(i).getBec().getMobile();
            member[2] = list.get(i).getBec().getBirth();
            member[3] = list.get(i).getBec().getGroup();
            member[4] = list.get(i).getBec().getNote();
            member[5] = list.get(i).getBec().getMailbox();
//         
            tableModel.addRow(member);
//            System.out.println("遍历成功2");
            jTable.invalidate();
        }
        

    }//GEN-LAST:event_jButton5ActionPerformed

    public int[] getSelectRow(int[] rows) {

        int[] select = new int[rows.length];
        for (int k = 0; k < rows.length; k++) {
            String name = jTable.getValueAt(rows[k], 0).toString();

            String phone = jTable.getValueAt(rows[k], 1).toString();

            for (int i = 1; i < list.size(); i++) {
                Basic b = list.get(i).getBec();
                if (b.getName().equals(name) && b.getMobile().equals(phone)) {
                    select[k] = i;

                }
            }//for i
        }//for k

        return select;
    }

    public static String delSpace(String str) {

        if (str == null) {
            return null;
        }
        String regStartSpace = "^[　 ]*";
        String regEndSpace = "[　 ]*$";

        // 连续两个 replaceAll
        // 第一个是去掉前端的空格， 第二个是去掉后端的空格
        String strDelSpace = str.replaceAll(regStartSpace, "").replaceAll(regEndSpace, "");
        return strDelSpace;
    }

    public static String[] divgroup(ArrayList<Info> a) {
        TreeSet<String> tr = new TreeSet<String>();
        for (int i = 0; i < a.size(); i++) {
            //group[i] = a.get(i).getBec().getGroup();
            if (a.get(i).getBec().getGroup() != null) {
                tr.add(a.get(i).getBec().getGroup());
            }
        }
        System.out.println(tr.size());
        String[] group = new String[tr.size() + 1];
        group[0] = "全部联系人";
        for (int i = 1; i < group.length; i++) {
            group[i] = tr.pollFirst();
        }
        return group;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;

                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                try {
                    UIManager.setLookAndFeel(new SubstanceLookAndFeel());
                    SubstanceLookAndFeel.setCurrentTheme(new SubstanceTerracottaTheme());
                    SubstanceLookAndFeel.setSkin(new EmeraldDuskSkin());
                    SubstanceLookAndFeel.setCurrentButtonShaper(new ClassicButtonShaper());
                    //设置水印
                    SubstanceBinaryWatermark watermark = new SubstanceBinaryWatermark();
                    SubstanceLookAndFeel.setCurrentWatermark(watermark);
                    //设置边框
                    SubstanceLookAndFeel.setCurrentBorderPainter(new StandardBorderPainter());
                    SubstanceLookAndFeel.setCurrentBorderPainter(new StandardBorderPainter());
                    SubstanceLookAndFeel.setCurrentGradientPainter(new StandardGradientPainter());
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                }

                JFrame.setDefaultLookAndFeelDecorated(true);
                JDialog.setDefaultLookAndFeelDecorated(true);
                SubstanceLookAndFeel.setCurrentTheme(new SubstanceTerracottaTheme());
                SubstanceLookAndFeel.setSkin(new EmeraldDuskSkin());
                SubstanceLookAndFeel.setCurrentButtonShaper(new ClassicButtonShaper());
                //设置水印
                SubstanceLookAndFeel.setCurrentWatermark(new SubstanceBinaryWatermark());
                //设置边框
                SubstanceLookAndFeel.setCurrentBorderPainter(new StandardBorderPainter());
                SubstanceLookAndFeel.setCurrentBorderPainter(new StandardBorderPainter());
                SubstanceLookAndFeel.setCurrentGradientPainter(new StandardGradientPainter());

                JFrame frame = new MainFrame();
                frame.setVisible(true);
                frame.setSize(700, 566);
                frame.setLocationRelativeTo(null);
                frame.setResizable(false);

               // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                Toolkit tk = Toolkit.getDefaultToolkit();
                Image image = tk.createImage("logo.png");
                frame.setIconImage(image);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem export;
    private javax.swing.JMenu importCSV;
    private javax.swing.JMenuItem importVCARD;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable;
    private javax.swing.JTextField search;
    // End of variables declaration//GEN-END:variables
    //public static DefaultTableModel tableModel;
}
